
#include "cloud_msg.h"
#include "cJSON.h"
#include "gw.h"
#include "wn_device.h"
#include "emu_common.h"
#include "emu_net.h"
#include <time.h>
 #define LOG_TAG              "cloud"
 #define LOG_LVL              LOG_LVL_DBG
#include <ulog.h>

msg_dealfuns emu_funs[]={
        {"emu_wn_req",   emu_wnParam_upload_topic,  RESPURL_NULL,               METHOD_UPLOAD,  NULL,           emu_wnParam_upload},
        {"cloud_setTime",cloud_setEmuTime_topic,    emu_resp_setEmuTime_topic,  METHOD_REQ,     emu_setTime,    NULL} ,       
        {"cloud_readInv",cloud_readInv_topic,       emu_resp_readInv_topic,     METHOD_REQ,     emu_readInv,    NULL},
        {"cloud_readEmu",cloud_readEmu_topic,       emu_resp_readEmu_topic,     METHOD_REQ,     emu_readEmu,    NULL},
        {"cloud_readAll",cloud_readAll_topic,       emu_resp_readAll_topic,     METHOD_REQ,     emu_readAll,    NULL},
        {"cloud_writeInv",cloud_writeInv_topic,     emu_resp_writeInv_topic,    METHOD_REQ,     emu_writeInv,   NULL},
        {"cloud_writeEmu",cloud_writeEmu_topic,     emu_resp_writeEmu_topic,    METHOD_REQ,     emu_writeEmu,   NULL},
        {"cloud_upgrade", cloud_upgrade_topic,      emu_resp_upgrade_topic,     METHOD_REQ,     emu_upgrade,    NULL},
        {"cloud_netSetting", cloud_netSetting_topic,emu_resp_netSetting_topic,  METHOD_REQ,     emu_netSetting, NULL},
        {"cloud_updatePwd", cloud_updatePwd_topic,  emu_resp_updatePwd_topic,   METHOD_REQ,     emu_updatePwd,  NULL},

};
//struct rt_completion g_wnota_completion;
msg_dealfuns* msg_getfun_node(const char* name,const char* url)
{
    for(int i=0;i<sizeof(emu_funs)/sizeof(msg_dealfuns);i++)
    {
        char requrl[64]={0};
        rt_sprintf(requrl,emu_funs[i].requrl,EMU_SN);
        if(rt_strcmp(emu_funs[i].name,name)==0 || strcmp(requrl,url)==0)
            return &emu_funs[i];
    }
    return NULL;
}

rt_uint32_t get_mid()
{
    static rt_uint32_t mid=1;
    return mid++;
}
int emu_wnParam_upload(rt_uint8_t*outdata)
{
    //1.获取wn数据
    cJSON *root = cJSON_CreateObject();

    gw_param* gwdata =  gw_getparam();
    int wn_size = 0;
    wn_device_t* wns = NULL;
    wns = wn_getdevices(wns,wn_size);
    LOG_D("start pack upload\n");
    //2.组包
    cJSON_AddStringToObject(root,   "frameType","0x03");
    cJSON_AddNumberToObject(root,   "mid", get_mid());
    //cJSON_AddNumberToObject(root,   "timeStamp", time(RT_NULL));
    cJSON *json_gwdata = cJSON_CreateObject();
    cJSON_AddStringToObject(json_gwdata,    "sn",gwdata->sn);
    cJSON_AddNumberToObject(json_gwdata,    "runningStatus", gwdata->runningStatus);
    cJSON_AddNumberToObject(json_gwdata,    "uploadInterval",gwdata->uploadInterval);
    cJSON_AddNumberToObject(json_gwdata,    "monitorNum", gwdata->monitorNum);
    cJSON_AddNumberToObject(json_gwdata,    "softVer",gwdata->softVer[0]);
    cJSON_AddNumberToObject(json_gwdata,    "hardVer",gwdata->hardVer[0]);
    cJSON_AddItemToObject(root, "gateWayData",json_gwdata);
    char* proot = cJSON_Print(root);
    cJSON *json_Invs = cJSON_CreateArray();
    for(int i=0;i<wn_size;i++)
    {
        if(wns[i].enable)
        {
            cJSON *json_wn = cJSON_CreateObject();
            cJSON_AddStringToObject(json_wn,    "sn",wns[i].device_sn);
            cJSON *json_wn_data = cJSON_CreateObject();
            cJSON_AddNumberToObject(json_wn_data,    "pvTempPower", wns[i].param_data.pvTempPower);
            cJSON_AddNumberToObject(json_wn_data,    "acTempPower", wns[i].param_data.acTempPower);
            cJSON_AddNumberToObject(json_wn_data,    "gridFreq",    wns[i].param_data.gridFreq);
            cJSON_AddNumberToObject(json_wn_data,    "sysStatus",   wns[i].param_data.sysStatus);
            cJSON_AddNumberToObject(json_wn_data,    "sysctlCmd",   wns[i].param_data.sysctlCmd);
            cJSON_AddNumberToObject(json_wn_data,    "pvVolReal",   wns[i].param_data.pvVolReal);
            cJSON_AddNumberToObject(json_wn_data,    "PvCurrReal",  wns[i].param_data.PvCurrReal);
            cJSON_AddNumberToObject(json_wn_data,    "acVolReal",   wns[i].param_data.acVolReal);
            cJSON_AddNumberToObject(json_wn_data,    "acCurrReal",  wns[i].param_data.acCurrReal);
            cJSON_AddNumberToObject(json_wn_data,    "acMosTempReal",   wns[i].param_data.acMosTempReal);
            cJSON_AddNumberToObject(json_wn_data,    "pVMosTempReal",   wns[i].param_data.pVMosTempReal);
            cJSON_AddNumberToObject(json_wn_data,    "dayRunTime",  wns[i].param_data.dayRunTime);
            cJSON_AddNumberToObject(json_wn_data,    "dailyEnergy", wns[i].param_data.dailyEnergy);
            cJSON_AddItemToObject(json_wn, "data",json_wn_data);
            cJSON_AddNumberToObject(json_wn,    "pvCount",wns[i].pvCount);
            cJSON_AddNumberToObject(json_wn,    "commStatus",wns[i].commStatus);
            cJSON_AddNumberToObject(json_wn,    "faultCode",wns[i].falutCode);
            cJSON_AddNumberToObject(json_wn,    "softVer",wns[i].softVer[0]);
            cJSON_AddNumberToObject(json_wn,    "hardVer",wns[i].hardVer[0]);
            cJSON_AddItemToArray(json_Invs, json_wn);
        //cJSON_Delete(json_wn);
        }
    }
    cJSON_AddItemToObject(root, "invData",json_Invs);
    char* od = cJSON_Print(root);
    LOG_D("outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)
        free(od);
    //cJSON_Delete(json_gwdata);
    cJSON_Delete(root);
    return EMU_SUCCESS;


}
int emu_readInv(rt_uint8_t *querydata,rt_uint8_t *outdata)
{
    wn_device_t* wns[250];
    rt_uint32_t device_num = 0;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        return EMU_ERROR;
    }
    cJSON* invData = cJSON_GetObjectItem(root, "invData");
    if(invData == NULL)
    {
        return EMU_ERROR;
    }
    cJSON* invs = cJSON_GetObjectItem(root, "Inv");
    if(cJSON_IsArray(invs))
    {
        int a_size = cJSON_GetArraySize(invs);
        for(int i=0;i<a_size;i++)
        {
            cJSON* item = cJSON_GetArrayItem(invs,i);
            if(cJSON_IsString(item))
            {
                char* sn = item->valuestring;
                printf("%s\n",sn);
                wns[device_num++] = wn_getdevice_fromSn(sn);
            }
        }
    }
    cJSON_Delete(root);
    //2.查找数据
    //3.组包
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x11");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   3550);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   "sn");
    cJSON *json_Invs = cJSON_CreateArray();
    for(int i=0;i<device_num;i++)
    {
        cJSON *json_wn = cJSON_CreateObject();
        cJSON_AddStringToObject(json_wn,    "sn",wns[i]->device_sn);
        cJSON *json_wn_data = cJSON_CreateObject();
        cJSON_AddNumberToObject(json_wn_data,    "pvTempPower", wns[i]->param_data.pvTempPower);
        cJSON_AddNumberToObject(json_wn_data,    "acTempPower", wns[i]->param_data.acTempPower);
        cJSON_AddNumberToObject(json_wn_data,    "gridFreq",    wns[i]->param_data.gridFreq);
        cJSON_AddNumberToObject(json_wn_data,    "sysStatus",   wns[i]->param_data.sysStatus);
        cJSON_AddNumberToObject(json_wn_data,    "sysctlCmd",   wns[i]->param_data.sysctlCmd);
        cJSON_AddNumberToObject(json_wn_data,    "pvVolReal",   wns[i]->param_data.pvVolReal);
        cJSON_AddNumberToObject(json_wn_data,    "PvCurrReal",  wns[i]->param_data.PvCurrReal);
        cJSON_AddNumberToObject(json_wn_data,    "acVolReal",   wns[i]->param_data.acVolReal);
        cJSON_AddNumberToObject(json_wn_data,    "acCurrReal",  wns[i]->param_data.acCurrReal);
        cJSON_AddNumberToObject(json_wn_data,    "acMosTempReal",   wns[i]->param_data.acMosTempReal);
        cJSON_AddNumberToObject(json_wn_data,    "pVMosTempReal",   wns[i]->param_data.pVMosTempReal);
        cJSON_AddNumberToObject(json_wn_data,    "dayRunTime",  wns[i]->param_data.dayRunTime);
        cJSON_AddNumberToObject(json_wn_data,    "dailyEnergy", wns[i]->param_data.dailyEnergy);
        cJSON_AddItemToObject(json_wn, "data",json_wn_data);
        cJSON_AddNumberToObject(json_wn,    "pvCount",wns[i]->pvCount);
        cJSON_AddNumberToObject(json_wn,    "commStatus",wns[i]->commStatus);
        cJSON_AddNumberToObject(json_wn,    "faultCode",wns[i]->falutCode);
        cJSON_AddNumberToObject(json_wn,    "softVer",wns[i]->softVer[0]);
        cJSON_AddNumberToObject(json_wn,    "hardVer",wns[i]->hardVer[0]);
        cJSON_AddItemToArray(json_Invs, json_wn);
        //cJSON_Delete(json_wn);
    }
    cJSON_AddItemToObject(response, "invData",json_Invs);
    LOG_D("outdata:%s\n",cJSON_Print(response));
    char* od = cJSON_Print(response);
    rt_memcpy(outdata,cJSON_Print(response),strlen(od));

    //cJSON_Delete(json_gwdata);
    cJSON_Delete(response);
    return EMU_SUCCESS;
}
int emu_setTime(uint8_t *querydata,uint8_t *outdata)
{
    LOG_D("req:%s\n",querydata);
    wn_device_t* wns[250];
    rt_uint32_t device_num = 0;
    char* str ;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* timezone = cJSON_GetObjectItem(root, "timeZone");
    if(timezone == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    int rmid = cJSON_GetNumberValue(req_mid);
    uint64_t sn = cJSON_GetNumberValue(snData);
    char* contry = cJSON_GetStringValue(cJSON_GetObjectItem(timezone,"contry"));
    char* timeCode = cJSON_GetStringValue(cJSON_GetObjectItem(timezone,"timeCode"));
    LOG_D("cloud set time zome:sn(%lld),contry(%s),timeCode(%s)\n",sn,contry,timeCode);
    //utc-8
    int tz = 0;
    if(timeCode[3] =='-')
    {
        //复数
        tz =0 - atoi(timeCode+4);
    }
    else{
        //正数
        tz = atoi(timeCode+4);
    }
    //调用接口设置
    LOG_D("cloud set time zome:sn(%lld),contry(%s),timeCode(%s),tz=%d\n",sn,contry,timeCode,tz);
    tz_set(tz);
    //cJSON_Delete(root);
    //2.查找数据
    //3.组包
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x05");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddNumberToObject(response,    "sn",   sn);
    cJSON_AddNumberToObject(response,    "status",  1);
    char* od = cJSON_Print(response);
    LOG_D("outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;
error:
    str ="paramerror";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}
int emu_readEmu(uint8_t *querydata,uint8_t *outdata)
{
    LOG_D("req:%s\n",querydata);
    char* str ;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    char* sn = cJSON_GetStringValue(snData);
    int rmid = cJSON_GetNumberValue(req_mid);
    //调用接口设置
    LOG_D("cloud read emu:sn(%s)\n",sn);

    cJSON_Delete(root);
    //2.查找数据
    gw_param* gwdata =  gw_getparam();
    //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x12");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON *json_gwdata = cJSON_CreateObject();
    cJSON_AddStringToObject(json_gwdata,    "sn",gwdata->sn);
    cJSON_AddNumberToObject(json_gwdata,    "runningStatus", gwdata->runningStatus);
    cJSON_AddNumberToObject(json_gwdata,    "uploadInterval",gwdata->uploadInterval);
    cJSON_AddNumberToObject(json_gwdata,    "monitorNum", gwdata->monitorNum);
    cJSON_AddNumberToObject(json_gwdata,    "softVer",gwdata->softVer[0]);
    cJSON_AddNumberToObject(json_gwdata,    "hardVer",gwdata->hardVer[0]);
    cJSON_AddItemToObject(response, "gateWayData",json_gwdata);
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}
int emu_readAll(uint8_t *querydata,uint8_t *outdata)
{
    LOG_D("req:%s\n",querydata);
    char* str ;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    char* sn = cJSON_GetStringValue(snData);
    int rmid = cJSON_GetNumberValue(req_mid);
    //调用接口设置
    LOG_D("cloud read emu:sn(%s)\n",sn);

    cJSON_Delete(root);
    //2.查找数据
    int wn_size = 0;
    wn_device_t* wns = NULL;
    wns = wn_getdevices(wns,wn_size);
    //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x11");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON *json_Invs = cJSON_CreateArray();
    for(int i=0;i<wn_size;i++)
    {
        if(wns[i].enable)
        {
            cJSON *json_wn = cJSON_CreateObject();
            cJSON_AddStringToObject(json_wn,    "sn",wns[i].device_sn);
            cJSON *json_wn_data = cJSON_CreateObject();
            cJSON_AddNumberToObject(json_wn_data,    "pvTempPower", wns[i].param_data.pvTempPower);
            cJSON_AddNumberToObject(json_wn_data,    "acTempPower", wns[i].param_data.acTempPower);
            cJSON_AddNumberToObject(json_wn_data,    "gridFreq",    wns[i].param_data.gridFreq);
            cJSON_AddNumberToObject(json_wn_data,    "sysStatus",   wns[i].param_data.sysStatus);
            cJSON_AddNumberToObject(json_wn_data,    "sysctlCmd",   wns[i].param_data.sysctlCmd);
            cJSON_AddNumberToObject(json_wn_data,    "pvVolReal",   wns[i].param_data.pvVolReal);
            cJSON_AddNumberToObject(json_wn_data,    "PvCurrReal",  wns[i].param_data.PvCurrReal);
            cJSON_AddNumberToObject(json_wn_data,    "acVolReal",   wns[i].param_data.acVolReal);
            cJSON_AddNumberToObject(json_wn_data,    "acCurrReal",  wns[i].param_data.acCurrReal);
            cJSON_AddNumberToObject(json_wn_data,    "acMosTempReal",   wns[i].param_data.acMosTempReal);
            cJSON_AddNumberToObject(json_wn_data,    "pVMosTempReal",   wns[i].param_data.pVMosTempReal);
            cJSON_AddNumberToObject(json_wn_data,    "dayRunTime",  wns[i].param_data.dayRunTime);
            cJSON_AddNumberToObject(json_wn_data,    "dailyEnergy", wns[i].param_data.dailyEnergy);
            cJSON_AddItemToObject(json_wn, "data",json_wn_data);
            cJSON_AddNumberToObject(json_wn,    "pvCount",wns[i].pvCount);
            cJSON_AddNumberToObject(json_wn,    "commStatus",wns[i].commStatus);
            cJSON_AddNumberToObject(json_wn,    "faultCode",wns[i].falutCode);
            cJSON_AddNumberToObject(json_wn,    "softVer",wns[i].softVer[0]);
            cJSON_AddNumberToObject(json_wn,    "hardVer",wns[i].hardVer[0]);
            cJSON_AddItemToArray(json_Invs, json_wn);
        //cJSON_Delete(json_wn);
        }
    }
    cJSON_AddItemToObject(response, "invData",json_Invs);
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}

int emu_writeInv(uint8_t *querydata,uint8_t *outdata)
{
    LOG_D("req:%s\n",querydata);
    char* str ;
    wn_device_t* wns[250];
    rt_uint32_t device_num = 0;
    int ret = -1;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    char* sn = cJSON_GetStringValue(snData);
    int rmid = cJSON_GetNumberValue(req_mid);
    //调用接口设置
    LOG_D("cloud read emu:sn(%s)\n",sn);
    cJSON* invs = cJSON_GetObjectItem(root, "invData");
    if(cJSON_IsArray(invs))
    {
        int a_size = cJSON_GetArraySize(invs);
        for(int i=0;i<a_size;i++)
        {
            cJSON* item = cJSON_GetArrayItem(invs,i);
            char*device_sn = cJSON_GetStringValue(cJSON_GetObjectItem(item,"sn"));
            cJSON* data = cJSON_GetObjectItem(item,"data");
            int data_size = cJSON_GetArraySize(data);
            for(int addrj = 0;addrj<data_size;addrj++)
            {
                cJSON* reg_item = cJSON_GetArrayItem(data,addrj);
                int reg_addr = cJSON_GetNumberValue(cJSON_GetObjectItem(reg_item,"addr"));
                int reg_val = cJSON_GetNumberValue(cJSON_GetObjectItem(reg_item,"value"));
                printf("addr:0x%02x,val:0x%02x\n",reg_addr,reg_val);
                ret = wn_device_writereg(device_sn,reg_addr,reg_val);
            }
        }
    }
   //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x12");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON_AddStringToObject(response,    "status",   ret);
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;    
    
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}

int emu_writeEmu(uint8_t *querydata,uint8_t* outdata)
{
    LOG_D("req:%s\n",querydata);
    char* str ;
    wn_device_t* wns[250];
    rt_uint32_t device_num = 0;
    int ret = -1;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    char* sn = cJSON_GetStringValue(snData);
    int rmid = cJSON_GetNumberValue(req_mid);
    //调用接口设置
    LOG_D("cloud read emu:sn(%s)\n",sn);
    cJSON* invs = cJSON_GetObjectItem(root, "invData");
    if(cJSON_IsArray(invs))
    {
        int a_size = cJSON_GetArraySize(invs);
        for(int i=0;i<a_size;i++)
        {
            cJSON* item = cJSON_GetArrayItem(invs,i);
            char*device_sn = cJSON_GetStringValue(cJSON_GetObjectItem(item,"sn"));
            cJSON* data = cJSON_GetObjectItem(item,"data");
            int data_size = cJSON_GetArraySize(data);
            for(int addrj = 0;addrj<data_size;addrj++)
            {
                cJSON* reg_item = cJSON_GetArrayItem(data,addrj);
                int reg_addr = cJSON_GetNumberValue(cJSON_GetObjectItem(reg_item,"addr"));
                int reg_val = cJSON_GetNumberValue(cJSON_GetObjectItem(reg_item,"value"));
                printf("addr:0x%02x,val:0x%02x\n",reg_addr,reg_val);
                ret = wn_device_writereg(device_sn,reg_addr,reg_val);
            }
        }
    }
   //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x12");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON_AddStringToObject(response,    "status",   ret);
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;    
    
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}

int emu_upgrade(uint8_t *querydata,uint8_t* outdata)
{
    LOG_D("req:%s\n",querydata);
    char* str ;
    int ret = -1;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* InvData = cJSON_GetObjectItem(root, "Inv");
    if(InvData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* emuData = cJSON_GetObjectItem(root, "Emu");
    if(emuData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    int rmid = cJSON_GetNumberValue(req_mid);
    char* sn = cJSON_GetStringValue(snData);
    LOG_D("rmid:%d,sn:%s\n", rmid, sn);
    //解析Inv 消息
    char* inv_url =  cJSON_GetStringValue(cJSON_GetObjectItem(InvData, "url"));
    char* inv_md5 = cJSON_GetStringValue(cJSON_GetObjectItem(InvData, "md5"));
    cJSON* inv_sns = cJSON_GetObjectItem(InvData, "sn");
    sn_set_t ins_sn[25];
    int sn_size = 0;
    LOG_D("before decode sns\n");
    if(cJSON_IsArray(inv_sns))
    {
        int a_size = cJSON_GetArraySize(inv_sns);
        for(int i=0;i<a_size;i++)
        {
                LOG_D("before decode sns2\n");
            cJSON* item = cJSON_GetArrayItem(inv_sns,i);
                LOG_D("before decode sns3\n");
            char* invsn = cJSON_GetStringValue(item);
                LOG_D("before decode sns4\n");
            rt_memcpy(ins_sn[sn_size++].sn,invsn,MIN(rt_strlen(invsn),sizeof(ins_sn[sn_size++].sn)));
        }
    }
    for(int j=0;j<sn_size;j++)
    {
        LOG_D("sn:%s\n",ins_sn[j].sn);
    }
        LOG_D("before decode sns5\n");
    //这里异步进行，避免堆栈溢出
    /* 初始化完成量对象 */
    //rt_completion_init(&g_wnota_completion);
    //wn_ota_upgrade_async(inv_url,inv_md5,ins_sn,sn_size);
    wn_ota_upgrade(inv_url,inv_md5,ins_sn,sn_size);
    //rt_completion_wait(&g_wnota_completion, RT_WAITING_FOREVER);
        LOG_D("before decode sns6\n");
    //解析emu
    char* emu_url =  cJSON_GetStringValue(cJSON_GetObjectItem(emuData, "url"));
    char* emu_md5 = cJSON_GetStringValue(cJSON_GetObjectItem(emuData, "md5"));
    //wn_ota_upgrade(inv_url,inv_md5,ins_sn,sn_size);
     //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x21");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",  0);// time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON_AddNumberToObject(response,    "type",   0);
    //0-升级成功，1-升级钟，2-升级失败
    cJSON_AddNumberToObject(response,    "status",   ret);
    cJSON_AddNumberToObject(response,    "progress",   ret);
    LOG_D("finished pack");
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;    
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}
int emu_netSetting(uint8_t *querydata,uint8_t* outdata)
{
   LOG_D("req:%s\n",querydata);
    char* str ;
    int ret = -1;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* ethData = cJSON_GetObjectItem(root, "eth");
    if(ethData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* wifiData = cJSON_GetObjectItem(root, "wifi");
    if(wifiData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* g4Data = cJSON_GetObjectItem(root, "4g");
    if(g4Data == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    char* sn = cJSON_GetStringValue(snData);
    int rmid = cJSON_GetNumberValue(req_mid);
    //调用接口设置
    LOG_D("cloud read emu:sn(%s)\n",sn);
    char* eth_ip = cJSON_GetStringValue(cJSON_GetObjectItem(ethData,"ip"));
    char* eth_dns = cJSON_GetStringValue(cJSON_GetObjectItem(ethData,"dns"));
    char* eth_gw = cJSON_GetStringValue(cJSON_GetObjectItem(ethData,"gateway"));

   //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x12");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON_AddStringToObject(response,    "status",   ret);
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;    
    
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}
int emu_updatePwd(uint8_t *querydata,uint8_t* outdata)
{
    LOG_D("req:%s\n",querydata);
    char* str ;
    int ret = -1;
    //1.解析数据
    if(querydata == NULL || querydata == "")
    {
        printf("quertdata null \n");
        goto error;
        return EMU_ERROR;
    }
    cJSON* root = cJSON_Parse(querydata);
    if(root == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* req_mid = cJSON_GetObjectItem(root, "mid");
    if(req_mid == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* snData = cJSON_GetObjectItem(root, "sn");
    if(snData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
    cJSON* pwdData = cJSON_GetObjectItem(root, "passward");
    if(pwdData == NULL)
    {
        goto error;
        return EMU_ERROR;
    }
   
    char* sn = cJSON_GetStringValue(snData);
    int rmid = cJSON_GetNumberValue(req_mid);
    //调用接口设置
    LOG_D("cloud read emu:sn(%s)\n",sn);
    char* password = cJSON_GetStringValue(pwdData);
    LOG_D("cloud passwd:sn(%s),passwd(%s)\n",sn,password);
   //3.组包
    LOG_D("start pack upload\n");
    cJSON* response = cJSON_CreateObject();
    cJSON_AddStringToObject(response,    "frameType",   "0x07");
    cJSON_AddNumberToObject(response,    "mid",         get_mid());
    cJSON_AddNumberToObject(response,    "reponseId",   rmid);
    cJSON_AddNumberToObject(response,    "timeStamp",   time(NULL));
    cJSON_AddStringToObject(response,    "sn",   sn);
    cJSON_AddStringToObject(response,    "status",   ret);
    char* od = cJSON_Print(response);
    LOG_D("emu_readEmu outdata:%s\n",od);
    rt_memcpy(outdata,od,strlen(od));
    if(od)  free(od);

    cJSON_Delete(response);
    return EMU_SUCCESS;    
    
error:
    str ="param error";
    rt_memcpy(outdata,str,strlen(str));
    return EMU_ERROR;
}